# ~*~ coding: utf-8 ~*~
from flask import request


class CompatibleWithSelect2Mixin(object):

    @staticmethod
    def select2_queryset():
        # 过滤
        filter_conditions = []
        search = request.values.get('search')
        filter_by_id = request.values.get('filter')
        if search:
            filter_conditions.append(('name', 'like', search))
        if filter_by_id:
            filter_conditions.append(('id', 'in', filter_by_id.split(',')))

        # 排序
        sort = request.values.get('sort') or 'create_time'
        order = request.values.get('order') or 'desc'
        order_by_list = [(sort, order), ]

        # 分页
        page = request.values.get('page')
        if page:
            limit = 12
            offset = (int(page) - 1) * limit
        else:
            limit = None
            offset = 0
        return filter_conditions, order_by_list, offset, limit
